package model.service;

import model.dao.AddDao;
import model.dao.AddDaoImpl;
import model.dao.BaseDao;
import model.entity.Book;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Date;

public class AddServiceImpl implements AddService{

    private AddDao addDao;
    private static JdbcTemplate template=new JdbcTemplate(BaseDao.getDataSource());

    public AddServiceImpl() {
        this.addDao = new AddDaoImpl(template);
    }

    public boolean isExist(String bookNo)//判断待添加的书籍是否存在
    {
        String sql="select count(1) from books where bookNo=?";
        int count=template.queryForObject(sql,new Object[]{bookNo},Integer.class);
        return count==1?true:false;
    }
    @Override
    public String add(Book book) {
        String bookNo=book.getBookNo();
        String msg="";
        if(isExist(bookNo))
            msg="书籍已存在";
        else
        {
            addDao.add(book);
            msg="添加成功";
        }

        return msg;
    }

    @Test
    public void test()
    {
        System.out.println("===================");
    }
}
